﻿using CefSharp;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UI = CPPEI.Coolzon.UI;

namespace Coolzon.PartLib.ECharts
{
    /// <summary>
    /// MapChina.xaml 的交互逻辑
    /// </summary>
    public partial class MapChina : ControlPart
    {
        private static CefSettings s_settings;

        private string _url;
        /// <summary>
        /// 阴影方向
        /// </summary>
        //[Category("Other")]
        //[DisplayName("Url地址")]
        //[Description("Url")]
        //[Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Url", "Url地址")]
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                if (_url != value)
                {
                    _url = value;
                    ReLoadPage();
                }
            }
        }

        private double _scaleX = 1.0d;
        private double _scaleY = 1.0d;
        [XProperty(XPropertyCategory.Other, "ScaleX", "ScaleX")]
        public double ScaleX
        {
            get
            {
                return _scaleX;
            }
            set
            {
                _scaleX = value;
            }
        }
        [XProperty(XPropertyCategory.Other, "ScaleY", "ScaleY地址")]
        public double ScaleY
        {
            get
            {
                return _scaleY;
            }
            set
            {
                _scaleY = value;
            }
        }

        private CefSharp.Wpf.ChromiumWebBrowser _webView;
        public MapChina()
        {
            InitializeComponent();

            if (s_settings == null)
            {
                s_settings = new CefSettings();
                //s_settings.SetOffScreenRenderingBestPerformanceArgs();
                Cef.Initialize(s_settings, true, false);
            }
            _webView = new CefSharp.Wpf.ChromiumWebBrowser();
            _webView.Background = Brushes.Black;
            
            this.Content = _webView;
            _webView.FrameLoadEnd += _webView_FrameLoadEnd;
        }

        void _webView_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
//            CefWindowHandle hWnd = browser->GetWindowHandle();
//DWORD dwOldStyle = ::GetWindowLongPtr(hWnd, GWL_STYLE);
//DWORD dwNewStyle = dwOldStyle & (~(WS_HSCROLL|WS_VSCROLL)) ;
 
//SetWindowLongPtr(hWnd, GWL_STYLE, dwNewStyle);
//ShowScrollBar(m_BrowserHwnd, SB_BOTH, FALSE);
            // System.Threading.Tasks.Task<double> task = _webView.GetZoomLevelAsync();
            // task.Wait();
            // double x = task.Result;
            //System.Diagnostics.Debug.WriteLine(string.Format("zoomlevel:{0}", x));
            //return;
            try
            {
                // 0.3333 = -6.03
                if (_scaleX != 1.0d || _scaleY != 1.0d)
                {

                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (_scaleX == 0.333333)
                        {
                            _webView.SetZoomLevel(-6.02);
                        }
                        else if (_scaleX == 0.666666)
                        {
                            _webView.SetZoomLevel(-2.19);
                        }
                        else
                        {
                            _webView.SetZoomLevel(0.0);
                        }
                    }));
                }
                else
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        _webView.SetZoomLevel(0);
                    }));
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected override void OnActive(PartContext context)
        {
            if(IsFirstActive)
            {
                /*
                Uri uri = new Uri(@"pack://application:,,,/Coolzon.PartLib.ECharts;component/Resources/mapchina.html");
                //using (Stream source = Application.GetResourceStream(uri).Stream)
                //{
                //    this.ctlWebBrowser.NavigateToStream(source);
                //}
                Stream source = Application.GetResourceStream(uri).Stream;
                this.ctlWebBrowser.NavigateToStream(source);
                */
                if (!string.IsNullOrEmpty(Url))
                {
                    _webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                    if (Url.ToUpper().StartsWith("HTTP://")
                        || Url.ToUpper().StartsWith("HTTPS://"))
                    {
                        _webView.Address = Url;
                    }
                    else
                    {
                        _webView.Address = SolutionResourceManager.GetAbsoluteFilePath(Url);
                    }
                }
            }
            base.OnActive(context);
        }

        protected override void OnDestory()
        {
            _webView.Dispose();
            base.OnDestory();
        }

        public override void RaiseScriptNetEvent(string eventName, string dataString)
        {
            try
            {
                _webView.ExecuteScriptAsync("jzcode.event.callback", eventName, dataString);
            }
            catch (Exception ex)
            {
                _webView.ExecuteScriptAsync("civs.event.callback", eventName, dataString);
            }
        }

        // 重新加载页面
        private void ReLoadPage()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                //_webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                if (Url.ToUpper().StartsWith("HTTP://")
                    || Url.ToUpper().StartsWith("HTTPS://"))
                {
                    _webView.Address = Url;
                }
                else
                {
                    _webView.Address = SolutionResourceManager.GetAbsoluteFilePath(Url);
                }
            }
        }
    }
}
